package com.wl156.activities;

import java.io.IOException;

import org.ksoap2.serialization.SoapPrimitive;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.wl156.BaseActivity;
import com.wl156.MyApp;
import com.wl156.activities.find.CarMessageInfo;
import com.wl156.activities.find.GoodInfo;
import com.wl156.activities.order.MyOrderDetail;
import com.wl156.lite.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.model.Wl156DB;

public class OneMessage extends BaseActivity {

	WebView mWebView;
	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;
	private boolean isCompany = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_one_message);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();
		isCompany = "C"
				.equals(((MyApp) getApplication()).getCurrentUser().type);

		try {
			Bundle extras = getIntent().getExtras();
			String msg = extras.getString("msg");
			Log.d("MessageCenter", msg);

			String[] line = msg.split("##");

			Wl156DB.markMessageRead(this, line[0]);

			mWebView = (WebView) findViewById(R.id.webview);
			// mWebView.setWebChromeClient(new WebChromeClient());
			mWebView.setWebViewClient(new MyWebViewClient());
			mWebView.loadDataWithBaseURL(null, line[4] == null ? "" : line[4],
					"text/html", "utf-8", null);
		} catch (Exception e) {
		}
	}

	class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// 10-24 21:56:45.242: D/MessageCenter(6294):
			// 4807##订单信息##您有订单单号8627290480-11024215544,从到等待您确认##2012-10-24
			// 21:55:45##您有订单单号8627290480-11024215544,从到等待您确认,<a
			// href="wl156://daijieshou">待接受</a>,<a
			// href="wl156://bujieshou">不接受</a>,详情点击<a
			// href="wl156://order_message/42">查看</a>详细信息

			try {
				if (url.startsWith("wl156")) {
					Class target = null;
					long id = 0;
					try {
						id = Long
								.parseLong(url.substring(url.lastIndexOf("/") + 1));
					} catch (Exception e) {
						return true;
					}
					final String strId = String.valueOf(id);
					if (url.contains("good_message")) {
						target = GoodInfo.class;
					} else if (url.contains("truck_message")) {
						target = CarMessageInfo.class;
					} else if (url.contains("order_message")) {
						Intent i = new Intent(OneMessage.this,
								MyOrderDetail.class);
						i.putExtra("id", strId);
						startActivity(i);
					} else if (url.contains("daijieshou")) {
						new AsyncTask<Void, Void, Object>() {
							ProgressDialog mDialog;

							protected void onPreExecute() {
								mDialog = ProgressDialog.show(OneMessage.this,
										"", "请稍后...");
							};

							protected void onPostExecute(Object result) {
								mDialog.dismiss();
								if (result instanceof SoapPrimitive) {
									SoapPrimitive sp = (SoapPrimitive) result;
									Toast.makeText(OneMessage.this,
											sp.toString(), Toast.LENGTH_SHORT)
											.show();
								} else {
									Toast.makeText(OneMessage.this, "成功",
											Toast.LENGTH_SHORT).show();
								}
							};

							@Override
							protected Object doInBackground(Void... params) {
								try {
									if (isCompany) {
										return mWebServiceClient
												.company_Status(mCertificate,
														strId, "待接受");
									} else {
										return mWebServiceClient.driver_Status(
												mCertificate, strId, "待接受");
									}
								} catch (IOException e) {
									e.printStackTrace();
									return e;
								} catch (XmlPullParserException e) {
									e.printStackTrace();
									return e;
								}
							}
						}.execute();
					} else if (url.contains("bujieshou")) {
						new AsyncTask<Void, Void, Object>() {
							ProgressDialog mDialog;

							protected void onPreExecute() {
								mDialog = ProgressDialog.show(OneMessage.this,
										"", "请稍后...");
							};

							protected void onPostExecute(Object result) {
								mDialog.dismiss();
								try {
									if (result instanceof SoapPrimitive) {
										SoapPrimitive sp = (SoapPrimitive) result;
										Toast.makeText(OneMessage.this,
												sp.toString(),
												Toast.LENGTH_SHORT).show();
									} else {
										Toast.makeText(OneMessage.this, "成功",
												Toast.LENGTH_SHORT).show();
									}
								} catch (Exception e) {
									Toast.makeText(OneMessage.this, "异常，稍后尝试",
											Toast.LENGTH_SHORT).show();
								}
							};

							@Override
							protected Object doInBackground(Void... params) {
								try {
									if (isCompany) {
										return mWebServiceClient
												.company_Status(mCertificate,
														strId, "不接受");
									} else {
										return mWebServiceClient.driver_Status(
												mCertificate, strId, "不接受");
									}
								} catch (IOException e) {
									e.printStackTrace();
									return e;
								} catch (XmlPullParserException e) {
									e.printStackTrace();
									return e;
								} catch (Exception e) {
									e.printStackTrace();
									return e;
								}
							}
						}.execute();
					}
					try {
						Intent i = new Intent(OneMessage.this, target);
						i.putExtra("id", id);
						startActivity(i);
					} catch (Exception e) {
					}
					return true;
				} else {
					return super.shouldOverrideUrlLoading(view, url);
				}
			} catch (Exception e) {
				return true;
			}
		}
	}
}
